package com.ratp.expertisetechnique;

import com.ratp.bean.Expertise;
import com.ratp.bean.UtilsBean;
import com.ratp.composants.CustomTextWatcher;
import com.ratp.expertisetechnique.R;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Essaie_Frein extends Activity {
	private RadioButton ButtonOuiDecelo;
	private RadioButton ButtonNonDecelo;
	private RadioGroup radioGroupDecelo;
	private EditText editSolSecEssaieDecelo;
	private EditText editValeurEssaieDecelo;
	private EditText editSolHumideEssaieDecelo;
	private EditText editValeurEssaieBanc;
	private EditText editSolSecEssaieBanc;
	private EditText editSolHumideEssaieBanc;
	private RadioButton ButtonOuiBanc;
	private RadioButton ButtonNonBanc;
	private RadioGroup radioGroupBranc;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.essaie_frein);
		initVariables();
		initEcouteurs();
	}
	private void initVariables() {
		ButtonOuiDecelo = (RadioButton) findViewById(R.id.ButtonOuiDecelo);
		ButtonNonDecelo = (RadioButton) findViewById(R.id.ButtonNonDecelo);
		radioGroupDecelo = (RadioGroup) findViewById(R.id.radioGroupDecelo);

		editValeurEssaieDecelo = (EditText) findViewById(R.id.editValeurEssaieDecelo);
		editSolSecEssaieDecelo = (EditText) findViewById(R.id.editSolSecEssaieDecelo);
		editSolHumideEssaieDecelo = (EditText) findViewById(R.id.editSolHumideEssaieDecelo);

		ButtonOuiBanc = (RadioButton) findViewById(R.id.ButtonOuiBanc);
		ButtonNonBanc = (RadioButton) findViewById(R.id.ButtonNonBanc);
		radioGroupBranc = (RadioGroup) findViewById(R.id.radioGroupBranc);

		editValeurEssaieBanc = (EditText) findViewById(R.id.editValeurEssaieBanc);
		editSolSecEssaieBanc = (EditText) findViewById(R.id.editSolSecEssaieBanc);
		editSolHumideEssaieBanc = (EditText) findViewById(R.id.editSolHumideEssaieBanc);


		Expertise expertise = UtilsBean.getExpertiseInstance();

		if(expertise.isDecelometre()){
			ButtonOuiDecelo.setChecked(true);
			ButtonNonDecelo.setChecked(false);
		}
		else{
			ButtonOuiDecelo.setChecked(false);
			ButtonNonDecelo.setChecked(true);
		}
		editValeurEssaieDecelo.setText(String.valueOf(expertise.getDecelometreValeur()));
		editSolSecEssaieDecelo.setText(String.valueOf(expertise.getDecelometreSec()));
		editSolHumideEssaieDecelo.setText(String.valueOf(expertise.getDecelometreHuimide()));

		if(expertise.isBanDeFrinage()){
			ButtonOuiBanc.setChecked(true);
			ButtonNonBanc.setChecked(false);
		}
		else{
			ButtonOuiBanc.setChecked(false);
			ButtonNonBanc.setChecked(true);
		}
		editValeurEssaieBanc.setText(String.valueOf(expertise.getBanDeFreinageValur()));
		editSolSecEssaieBanc.setText(String.valueOf(expertise.getBanDeFreinageSec()));
		editSolHumideEssaieBanc.setText(String.valueOf(expertise.getBanDFreinageHuimide()));
	}

	private void initEcouteurs() {
		radioGroupDecelo.setOnCheckedChangeListener(new OnCheckedChangeListener() 
		{
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				if (ButtonOuiDecelo.isChecked())
				{
					UtilsBean.getExpertiseInstance().setDecelometre(true);
				}
				else if(ButtonNonDecelo.isChecked())  {
					UtilsBean.getExpertiseInstance().setDecelometre(false);
				}
			}
		});
		editValeurEssaieDecelo.addTextChangedListener(new CustomTextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				String value = s.toString();
				if (value.length() != 0) {
					UtilsBean.getExpertiseInstance().setDecelometreValeur(
							Integer.parseInt(value));
				}
			}
		});
		editSolSecEssaieDecelo.addTextChangedListener(new CustomTextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				String value = s.toString();
				if (value.length() != 0) {
					UtilsBean.getExpertiseInstance().setDecelometreSec(Integer.parseInt(value));
				}
			}
		});
		editSolHumideEssaieDecelo.addTextChangedListener(new CustomTextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				String value = s.toString();
				if (value.length() != 0) {
					UtilsBean.getExpertiseInstance().setDecelometreHuimide(Integer.parseInt(value));
				}
			}
		});
		radioGroupBranc.setOnCheckedChangeListener(new OnCheckedChangeListener() 
		{
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				if (ButtonOuiBanc.isChecked())
				{
					UtilsBean.getExpertiseInstance().setBanDeFrinage(true);
				}
				else if(ButtonNonBanc.isChecked())  {
					UtilsBean.getExpertiseInstance().setBanDeFrinage(false);
				}
			}
		});
		editValeurEssaieBanc.addTextChangedListener(new CustomTextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				String value = s.toString();
				if (value.length() != 0) {
					UtilsBean.getExpertiseInstance().setBanDeFreinageValur(Integer.parseInt(value));
				}
			}
		});
		editSolSecEssaieBanc.addTextChangedListener(new CustomTextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				String value = s.toString();
				if (value.length() != 0) {
					UtilsBean.getExpertiseInstance().setBanDeFreinageSec(Integer.parseInt(value));
				}
			}
		});
		editSolHumideEssaieBanc.addTextChangedListener(new CustomTextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {
				String value = s.toString();
				if (value.length() != 0) {
					UtilsBean.getExpertiseInstance().setBanDFreinageHuimide(Integer.parseInt(value));
				}
			}
		});
	}


}
